import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

typedef void Change(int count);

class PlusSub extends StatefulWidget {
  final Change onChange;

  const PlusSub({Key key, this.onChange}) : super(key: key);

  @override
  _PlusSubState createState() => _PlusSubState();
}

class _PlusSubState extends State<PlusSub> {
  TextEditingController textEditingController;
  @override
  void initState() {
    textEditingController = TextEditingController(text: "12");
    super.initState();
  }

  @override
  void dispose() {
    textEditingController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: <Widget>[
        GestureDetector(
          onTap: () {
            cal(isAdd: false);
          },
          child: Container(
            decoration: BoxDecoration(color: Colors.grey.withOpacity(0.2)),
            height: 20,
            width: 20,
            child: Center(child: Text('-')),
          ),
        ),
        SizedBox(
          width: 1,
        ),
        Container(
            decoration: BoxDecoration(color: Colors.grey.withOpacity(0.3)),
            height: 20,
            width: 40,
            child: TextField(
              controller: textEditingController,
              keyboardAppearance: Brightness.light,
              textInputAction: TextInputAction.done,
              textAlign: TextAlign.center,
              style: TextStyle(fontSize: 13),
              cursorWidth: 2,
              showCursor: false,
              //readOnly: true,
              //toolbarOptions: ,
              cursorRadius: Radius.circular(5),
              maxLength: 4,
              maxLengthEnforced: true,
              keyboardType: TextInputType.number,
              decoration: InputDecoration(
                counterText: "",
                contentPadding: const EdgeInsets.all(2),
                border: InputBorder.none,
              ),
            )),
        SizedBox(
          width: 1,
        ),
        GestureDetector(
          onTap: () {
            cal();
          },
          child: Container(
            decoration: BoxDecoration(color: Colors.grey.withOpacity(0.2)),
            height: 20,
            width: 20,
            child: Center(child: Text('+')),
          ),
        ),
      ],
    );
  }

  cal({bool isAdd = true}) {
    
    var value = int.parse(textEditingController.text ?? "0");
    isAdd ? value++ : value--;
    if (value < 0) {
      value = 0;
    }
    textEditingController.text = value.toString();
  }
}
